﻿using System;
using System.Collections.Generic;
using System.Text;
using PPAP.Model;
using System.Data;
using System.Data.SqlClient;
using System.Data.OracleClient;

namespace PPAP.DAO
{
    public class PartBaseInfoDAO
    {
        public PartBaseInfoDAO() { }

        /// <summary>
        /// 增加一条数据 返回ID
        /// </summary>
        public int Add(PartBaseInfo model)
        {
            model.ID = DbHelperOra.GetMaxID("ID", "PARTBASEINFO");
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into PARTBASEINFO(");
            strSql.Append("ID,BDNTPARTNO,BDNTPARTNAME,PARTCOLOR,ELEVEL,QLEVEL,PLANNEDISIRDATE,CREATEDATE,CREATEUSER,SUBMISSIONLEVEL)");
            strSql.Append(" values (");
            strSql.Append(":ID,:BDNTPARTNO,:BDNTPARTNAME,:PARTCOLOR,:ELEVEL,:QLEVEL,:PLANNEDISIRDATE,:CREATEDATE,:CREATEUSER,:SUBMISSIONLEVEL)");
            OracleParameter[] parameters = {
					new OracleParameter(":ID", OracleType.Number,4),
					new OracleParameter(":BDNTPARTNO", OracleType.VarChar,20),
					new OracleParameter(":BDNTPARTNAME", OracleType.VarChar,50),
					new OracleParameter(":PARTCOLOR", OracleType.VarChar,20),
					new OracleParameter(":ELEVEL", OracleType.Number,4),
					new OracleParameter(":QLEVEL", OracleType.Number,4),
					new OracleParameter(":PLANNEDISIRDATE", OracleType.DateTime),
					new OracleParameter(":CREATEDATE", OracleType.DateTime),
					new OracleParameter(":CREATEUSER", OracleType.VarChar,20),
					new OracleParameter(":SUBMISSIONLEVEL", OracleType.VarChar,50)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.BDNTPartNo;
            parameters[2].Value = model.BDNTPartName;
            parameters[3].Value = model.PartColor;
            parameters[4].Value = model.ELevel;
            parameters[5].Value = model.QLevel;
            parameters[6].Value = model.PlannedISIRDate;
            parameters[7].Value = model.CreateDate;
            parameters[8].Value = model.CreateUser;
            parameters[9].Value = model.SubmissionLevel;

          return  DbHelperOra.ExecuteSql(strSql.ToString(), parameters)>0?model.ID:0;
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int Update(PartBaseInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update PARTBASEINFO set ");
            strSql.Append("ID=:ID,");
            strSql.Append("BDNTPARTNO=:BDNTPARTNO,");
            strSql.Append("BDNTPARTNAME=:BDNTPARTNAME,");
            strSql.Append("PARTCOLOR=:PARTCOLOR,");
            strSql.Append("ELEVEL=:ELEVEL,");
            strSql.Append("QLEVEL=:QLEVEL,");
            strSql.Append("PLANNEDISIRDATE=:PLANNEDISIRDATE,");
            strSql.Append("CREATEDATE=:CREATEDATE,");
            strSql.Append("CREATEUSER=:CREATEUSER,");
            strSql.Append("SUBMISSIONLEVEL=:SUBMISSIONLEVEL");
            strSql.Append(" where id="+model.ID);
            OracleParameter[] parameters = {
					new OracleParameter(":ID", OracleType.Number,4),
					new OracleParameter(":BDNTPARTNO", OracleType.VarChar,20),
					new OracleParameter(":BDNTPARTNAME", OracleType.VarChar,50),
					new OracleParameter(":PARTCOLOR", OracleType.VarChar,20),
					new OracleParameter(":ELEVEL", OracleType.Number,4),
					new OracleParameter(":QLEVEL", OracleType.Number,4),
					new OracleParameter(":PLANNEDISIRDATE", OracleType.DateTime),
					new OracleParameter(":CREATEDATE", OracleType.DateTime),
					new OracleParameter(":CREATEUSER", OracleType.VarChar,20),
					new OracleParameter(":SUBMISSIONLEVEL", OracleType.VarChar,50)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.BDNTPartNo;
            parameters[2].Value = model.BDNTPartName;
            parameters[3].Value = model.PartColor;
            parameters[4].Value = model.ELevel;
            parameters[5].Value = model.QLevel;
            parameters[6].Value = model.PlannedISIRDate;
            parameters[7].Value = model.CreateDate;
            parameters[8].Value = model.CreateUser;
            parameters[9].Value = model.SubmissionLevel;

           return DbHelperOra.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public int Delete(int id)
        {
            //该表无主键信息，请自定义主键/条件字段
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from PARTBASEINFO ");
            strSql.Append(" where id="+id);
            OracleParameter[] parameters = {
};

           return DbHelperOra.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public PartBaseInfo GetModel(int id)
        {
            //该表无主键信息，请自定义主键/条件字段
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,BDNTPARTNO,BDNTPARTNAME,PARTCOLOR,ELEVEL,QLEVEL,PLANNEDISIRDATE,CREATEDATE,CREATEUSER,SUBMISSIONLEVEL from PARTBASEINFO ");
            strSql.Append(" where id="+id);
            OracleParameter[] parameters = {
};

            PartBaseInfo model = new PartBaseInfo();
            DataSet ds = DbHelperOra.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                model.BDNTPartNo = ds.Tables[0].Rows[0]["BDNTPARTNO"].ToString();
                model.BDNTPartName = ds.Tables[0].Rows[0]["BDNTPARTNAME"].ToString();
                model.PartColor = ds.Tables[0].Rows[0]["PARTCOLOR"].ToString();
                if (ds.Tables[0].Rows[0]["ELEVEL"].ToString() != "")
                {
                    model.ELevel = int.Parse(ds.Tables[0].Rows[0]["ELEVEL"].ToString());
                }
                if (ds.Tables[0].Rows[0]["QLEVEL"].ToString() != "")
                {
                    model.QLevel = int.Parse(ds.Tables[0].Rows[0]["QLEVEL"].ToString());
                }
                if (ds.Tables[0].Rows[0]["PLANNEDISIRDATE"].ToString() != "")
                {
                    model.PlannedISIRDate = DateTime.Parse(ds.Tables[0].Rows[0]["PLANNEDISIRDATE"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CREATEDATE"].ToString() != "")
                {
                    model.CreateDate = DateTime.Parse(ds.Tables[0].Rows[0]["CREATEDATE"].ToString());
                }
                model.CreateUser = ds.Tables[0].Rows[0]["CREATEUSER"].ToString();
                model.SubmissionLevel = ds.Tables[0].Rows[0]["SUBMISSIONLEVEL"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,BDNTPARTNO,BDNTPARTNAME,PARTCOLOR,ELEVEL,QLEVEL,PLANNEDISIRDATE,CREATEDATE,CREATEUSER,SUBMISSIONLEVEL ");
            strSql.Append(" FROM PARTBASEINFO ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperOra.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            OracleParameter[] parameters = {
                    new OracleParameter(":tblName", OracleType.VarChar, 255),
                    new OracleParameter(":fldName", OracleType.VarChar, 255),
                    new OracleParameter(":PageSize", OracleType.Number),
                    new OracleParameter(":PageIndex", OracleType.Number),
                    new OracleParameter(":IsReCount", OracleType.Clob),
                    new OracleParameter(":OrderType", OracleType.Clob),
                    new OracleParameter(":strWhere", OracleType.VarChar,1000),
                    };
            parameters[0].Value = "PARTBASEINFO";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperOra.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/
    }
}
